package com.zl.learning.base64;

import org.junit.Test;

import java.io.*;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;

public class Base64Test {
    public static void main(String[] args) {
        String strImg = getImageStr();
        System.out.println(strImg);
        GenerateImage(strImg);
    }

    //图片转化成base64字符串
    public static String getImageStr() {//将图片文件转化为字节数组字符串，并对其进行Base64编码处理
        String imgFile = "排序算法/PANORAMAIMAGE1.jpg";//待处理的图片
        byte[] data = null;
        //读取图片字节数组
        try (InputStream in = new FileInputStream(imgFile);) {
            data = new byte[in.available()];
            in.read(data);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //对字节数组Base64编码
        return new String(Base64.getEncoder().encode(data));//返回Base64编码过的字节数组字符串
    }

    //base64字符串转化成图片
    public static boolean GenerateImage(String imgStr) {   //对字节数组字符串进行Base64解码并生成图片
        //图像数据为空
        if (imgStr == null) {
            return false;
        }
        //生成jpeg图片
        String imgFilePath = "排序算法/new.jpg";//新生成的图片
        try (OutputStream out = new FileOutputStream(imgFilePath);) {
            //Base64解码
            byte[] b = Base64.getDecoder().decode(imgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {//调整异常数据
                    b[i] += 256;
                }
            }
            out.write(b);
            out.flush();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Test
    public void test2() {
        String str = "beY6CDG/vX71IYXG5WsB38+Zh/FfQ8WD7qiiV0xmkjVTnkwkiYq5Qmmsvl3O/Rv+6QgJq6Qr8wDeSNkmJGHUwdRZyuLwWH+sJSsiLYJD9fBKXowW30Xgl0yZXIYVT4J+";
        String msg = new String(Base64.getDecoder().decode(str.getBytes()), Charset.forName("UTF-8"));
        System.out.println(msg);
    }

    @Test
    public void test3() throws UnsupportedEncodingException {
        String str = "JUU0JUJEJUEwJUU1JUE1JUJEJUU0JUI4JUFEJUU1JTlCJUJE";
        String url = new String(Base64.getDecoder().decode(str));
        String s = URLDecoder.decode(url, "UTF-8");
        System.out.println(s);
    }

    @Test
    public void test4() throws IOException {
        InputStream in = Base64Test.class.getResourceAsStream("/base64.txt");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] bytes = new byte[1024];
        int len = -1;
        while ((len = in.read(bytes)) != -1) {
            out.write(bytes, 0, len);
        }
        String str = out.toString();
        final String[] split = str.split(";base64,");
        System.out.println(split);
        String fileNmae = split[0].split("/")[1];
        final byte[] decode = Base64.getDecoder().decode(split[1]);
        Files.write(Paths.get("D:\\", "1." + fileNmae), decode);
    }

    @Test
    public void test5() {
        Integer i = 0;
        System.out.println(i instanceof Number);
    }
}